package com.blog.message.api.MQconsumer;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.blog.common.core.exception.ServiceException;
import com.blog.common.domain.entity.Comment;
import com.blog.common.mapper.dao.CommentMapper;
import com.blog.common.mapper.dao.MessageMapper;
import com.blog.message.api.constant.QueueConstant;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

import static com.blog.common.core.constant.ServiceConst.IS_TRUE;
import static com.blog.message.api.constant.QueueConstant.*;

/**
 * <p>
 * 评论消费者
 * </p>
 *
 * @author Valerie
 * @date 2023/5/27
 **/
@RabbitListener(
        ackMode = MANUAL,
        bindings = @QueueBinding(
        value = @Queue(
                name = QueueConstant.COMMENT_QUEUE,
                arguments = {
                        @Argument(name = "x-dead-letter-exchange", value = "dead_exchange"),
                        @Argument(name = "x-dead-letter-routing-key", value = "dead")
                },
                durable = TRUE),
        exchange = @Exchange(
                name = TOPIC_EXCHANGE,
                durable = FALSE,
                type = ExchangeTypes.TOPIC),
        key = COMMENT_KEY
))
@Component
@Slf4j
@RequiredArgsConstructor
public class CommentConsumer {

    private final CommentMapper commentMapper;

    private final MessageMapper messageMapper;

    /**
     * 审核评论
     *
     * @param comment 评论
     * @param message 消息
     * @param channel 信道
     * @return void
     */
    @RabbitHandler
    @Transactional(rollbackFor = Exception.class)
    public void review(Comment comment, Message message, Channel channel) throws IOException {
        log.info(comment.getCommentContent());
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        boolean update = new LambdaUpdateChainWrapper<>(commentMapper)
                .eq(Comment::getId, comment.getId())
                .set(Comment::getIsReview, IS_TRUE)
                .update();
        channel.basicAck(deliveryTag, false);
        if (!update){
            log.error("评论审核失败!");
        }
    }

    /**
     * 审核留言
     *
     * @param barrage 弹幕
     * @param message 消息
     * @param channel 信道
     * @return void
     */
    @RabbitHandler
    @Transactional(rollbackFor = Exception.class)
    public void review(com.blog.common.domain.entity.Message barrage, Message message, Channel channel) throws IOException {
        log.info(barrage.getMessageContent());
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        boolean update = new LambdaUpdateChainWrapper<>(messageMapper)
                .eq(com.blog.common.domain.entity.Message::getId, barrage.getId())
                .set(com.blog.common.domain.entity.Message::getIsReview, IS_TRUE)
                .update();
        channel.basicAck(deliveryTag, false);
        if (!update){
            log.error("留言审核失败!");
        }
    }

}
